package com.chengyu.core.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengyu.core.entity.TblMemberEntity;
import com.chengyu.core.mapper.TblMemberMapper;
import com.chengyu.core.request.TblMemberReq;
import com.chengyu.core.service.TblMemberService;
import com.chengyu.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户信息表
 *
 * @author zhyg dengzhiming@dhcc.com.cn
 * @since 3.0 2022-03-01
 */
@Service
@Slf4j
public class TblMemberServiceImpl extends ServiceImpl<TblMemberMapper, TblMemberEntity> implements TblMemberService {
    @Override
    public List<TblMemberReq> getAllUUid() {
        return this.baseMapper.getAllUUid();
    }

    /***
     * @auth
     * 根据前端传递过来的数据，
     * 检测到数据已经存在，显示插入成功
     * 如果信息不存在，则插入 并返回插入结果
     * @param tblMemberReq
     * @return int 0  或者 1
     */
    @Override
    public int insertTblMember(TblMemberReq tblMemberReq) {
        //g根据传过来的用户id 和 uuid 检测数据是否存在 存在则跳过
        int id = 0;
        TblMemberEntity tblMemberEntity1 = new TblMemberEntity();
//        tblMemberEntity1.setUuid(tblMemberReq.getUuid());
        tblMemberEntity1.setPhone(tblMemberReq.getPhone());
        List<TblMemberEntity> list = baseMapper.selectList(buildQueryWrapper(tblMemberEntity1));
        if (list == null || list.size() == 0) {
            tblMemberEntity1.setUuid(tblMemberReq.getUuid());
            id = baseMapper.insert(tblMemberEntity1);
        } else {
            tblMemberEntity1.setUuid(tblMemberReq.getUuid());
            id = baseMapper.updateUuidByPhone(tblMemberEntity1);
        }
        return id;
    }

    public Wrapper<TblMemberEntity> buildQueryWrapper(TblMemberEntity user) {
        QueryWrapper<TblMemberEntity> wrapper = Wrappers.query();
        wrapper.eq(StringUtils.isNotBlank(user.getPhone()), "phone", user.getPhone())
                .eq(StringUtils.isNotBlank(user.getUuid()), "uuid", user.getUuid());
        return wrapper;
    }


}
